﻿using BCCommon.PaymentEnums;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace BCCommon
{
    public static class Compare
    {
        /// <summary>
        /// 环比
        /// </summary>
        /// <param name="number"></param>
        /// <param name="monthNumber"></param>
        /// <param name="isIncrease"></param>
        /// <param name="increaseRate"></param>
        /// <returns></returns>
        public static void MonthOnMonth(decimal number, decimal monthNumber, out bool isIncrease,out string increaseRate)
        {
            isIncrease = number > monthNumber;
            if (monthNumber == 0)
            {
                if (number - monthNumber==0) increaseRate = "0";
                else increaseRate = Math.Abs((number - monthNumber) * 100) + "%";//本期减上月取绝对值
            }
            else
            {
                if (number - monthNumber == 0) increaseRate = "0";
                else increaseRate = Math.Abs(Math.Round((number - monthNumber) / monthNumber * 100, 2)) + "%";
            }
        }
        /// <summary>
        /// 同比
        /// </summary>
        /// <param name="number">当前值</param>
        /// <param name="yearNumber">上年值</param>
        /// <param name="isIncrease">是否增长</param>
        /// <param name="increaseRate">增长率</param>
        /// <returns></returns>
        public static void YearOnMonth(decimal number, decimal yearNumber, out bool isIncrease, out string increaseRate)
        {
            isIncrease = number > yearNumber;
            if (yearNumber == 0)
            {
                if (number - yearNumber == 0) increaseRate = "0";
                else increaseRate = Math.Abs((number - yearNumber) * 100) + "%";//本期减上月取绝对值
            }
            else
            {
                if (number - yearNumber == 0) increaseRate = "0";
                else increaseRate = Math.Abs(Math.Round((number - yearNumber) / yearNumber * 100, 2)) + "%";
            }
        }
        /// <summary>
        /// 密码
        /// </summary>
        /// <param name="password"></param>
        /// <param name="phone"></param>
        /// <returns></returns>
        public static string GetPassword(string password,string phone)
        {
            return password.SHA256(phone);
        }
        /// <summary>
        /// 取
        /// </summary>
        /// <param name="upNum"></param>
        /// <param name="downNum"></param>
        /// <returns></returns>
        public static string GetRate(decimal upNum, decimal downNum)
        {
            if (downNum==0) return "0";
            if (upNum == 0) return "0";
            return Math.Round(upNum / downNum * 100, 2) + "%";
        }

        /// <summary>
        /// 写记录
        /// </summary>
        /// <param name="record"></param>
        public static void WriteRecord(string record)
        {
            string sFilePath = Directory.GetCurrentDirectory();
            string sFileName = "record.log";
            sFileName = sFilePath + "\\" + sFileName; //文件的绝对路径
            if (!Directory.Exists(sFilePath))//验证路径是否存在
            {
                Directory.CreateDirectory(sFilePath);
                //不存在则创建
            }
            FileStream fs;
            using ((StreamWriter) null)
            {
                fs = File.Exists(sFileName) ? new FileStream(sFileName, FileMode.Append) : new FileStream(sFileName, FileMode.Create);
                var sw = new StreamWriter(fs);
                sw.WriteLine(record);
                sw.Close();
            }

            fs.Close();

        }


        /// <summary>
        /// 获取风险级别
        /// </summary>
        /// <param name="amount">提现金额</param>
        /// <returns></returns>
        public static SourceRiskLevelType GetRiskLevel(decimal amount)
        {
            if (amount >= 1000 && amount < 5000)
            {
                return SourceRiskLevelType.Low;
            }
            else if (amount >= 5000 && amount < 10000)
            {
                return SourceRiskLevelType.Middle;
            }
            else if (amount > 10000)
            {
                return SourceRiskLevelType.High;
            }
            else
            {
                return SourceRiskLevelType.Normal;
            }
        }

    }
}
